﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Safebrowsing
{
    /// <summary>
    /// Response entry containing full-length hashes.
    /// Look here for details: http://code.google.com/apis/safebrowsing/developers_guide_v2.html#HTTPResponseForHashes
    /// </summary>
    public class HashResponseEntry
    {
        /// <summary>
        /// Creates an instance of the HashResponseEntry
        /// </summary>
        /// <param name="list">List</param>
        /// <param name="addChunkNumber">Add chunk number</param>
        /// <param name="hashData">Full length hashes in binary</param>
        public HashResponseEntry(SafebrowsingList list, int addChunkNumber, byte[] hashData)
        {
            List = list;
            AddChunkNumber = addChunkNumber;
            Hashes = new List<string>();
            int count = hashData.Length/32;
            for (int i = 0; i < count; i++)
            {
                var hash = new byte[32];
                Array.Copy(hashData, i * 32, hash, 0, 32);
                Hashes.Add(Utils.ByteToHex(hash));
            }
        }

        public SafebrowsingList List { get; private set; }

        public int AddChunkNumber { get; private set; }

        public List<string> Hashes { get; private set; }

        public override string ToString()
        {
            var sb = new StringBuilder();
            sb.Append(List.Name);
            sb.Append(",");
            sb.Append(AddChunkNumber);
            sb.Append(":");

            foreach (string hash in Hashes)
            {
                sb.Append(hash);
                sb.Append(",");
            }

            sb.Remove(sb.Length - 1, 1);
            return sb.ToString();
        }
    }
}
